﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FSS.Core.Controls;
using Ext.Net;
using FSS.Core.Util;
using FSS.BLL.Services;
using FSS.BLL.Entities;

namespace SalesManagement.Modules.System
{
    public partial class Permission : BasePage
    {
        public Permission()
            : base(SCREENS.PERMISSION)
        {

        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        [DirectMethod]
        public void DocumentReady()
        {
            BindingRole();
        }
        private void BindingRole()
        {
            RoleService roleService = RoleService.GetInstance();
            IList<RoleEntity> lstRole = roleService.LoadAll();

            storeRole.DataSource = lstRole;
            storeRole.DataBind();
            cboRole.SelectedIndex = 0;
        }

        protected void storeMain_OnRefreshDate(object sender, StoreRefreshDataEventArgs e)
        {
            if (this.cboRole.SelectedItem.Value == null) return;

            string strRoleId = this.cboRole.SelectedItem.Value.ToString();
            int roleId = 0;
            int.TryParse(strRoleId, out roleId);
            if (roleId <= 0)
            {
                return;
            }
            PermissionService permissionService = PermissionService.GetInstance();
            PermissionEntity permissionEntity = new PermissionEntity();
            permissionEntity.RoleId = roleId;
            IList<PermissionEntity> lstPermission = permissionService.LoadByRole(permissionEntity);
            if (lstPermission == null) return;
            storeMain.DataSource = lstPermission;
            storeMain.DataBind();


        }

        protected void grdMain_OnAfterEdit(object sender, DirectEventArgs e)
        {
            try
            {
                string ScreenId = e.ExtraParams["ScreenId"].ToString();
                int RoleId = int.Parse(e.ExtraParams["RoleId"]);
                bool IsAdd = bool.Parse(e.ExtraParams["IsAdd"]);
                bool IsView = bool.Parse(e.ExtraParams["IsView"]);
                bool IsDelete = bool.Parse(e.ExtraParams["IsDelete"]);
                bool IsEdit = bool.Parse(e.ExtraParams["IsEdit"]);
                bool IsPrint = bool.Parse(e.ExtraParams["IsPrint"]);
                bool IsImport = bool.Parse(e.ExtraParams["IsImport"]);
                bool IsExport = bool.Parse(e.ExtraParams["IsExport"]);
                PermissionEntity permissionEntity = new PermissionEntity();
                permissionEntity.ScreenId = ScreenId;
                permissionEntity.RoleId = RoleId;
                permissionEntity.IsView = IsView;
                permissionEntity.IsAdd = IsAdd;
                permissionEntity.IsDelete = IsDelete;
                permissionEntity.IsEdit = IsEdit;
                permissionEntity.IsPrint = IsPrint;
                permissionEntity.IsImport = IsImport;
                permissionEntity.IsExport = IsExport;
                PermissionService permissionService = PermissionService.GetInstance();
                permissionService.Update(permissionEntity);
            }
            catch (Exception ex)
            {

            }
        }
        protected void grdMain_OnCommand(object sender, DirectEventArgs e) { }
        public void btnAdd_Click(object sender, DirectEventArgs e)
        {
            // this.WindowEditor.Show();
        }
        public void btnEdit_Click(object sender, DirectEventArgs e) { }
        public void btnDelete_Click(object sender, DirectEventArgs e) { }
        public void btnPrint_Click(object sender, DirectEventArgs e) { }
        public void btnImport_Click(object sender, DirectEventArgs e) { }
        public void btnExport_Click(object sender, DirectEventArgs e) { }
    }
}
